/*
 * Created on Jun 2, 2004
 */
package org.vectrics.recruiter.struts.tag;

import java.util.Iterator;
import java.util.List;
import org.apache.ecs.html.Input;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.company.CompanyPersonGroupType;
import org.vectrics.company.CompanyPersonRole;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;
import org.vectrics.user.UserPrivilegeType;

/**
 * @author mmoore
 */
public class CompanyGroupTypeSelectorTag extends VectricsTagSupport {

	private static final long serialVersionUID = 1552731358905625944L;
	
	private String showBgColor = "false";
	private boolean alternateColorRow = false; 
	private String maxCompanyCount = "30";
	private int columnCount = 2;
	private String companyPersonRoleId = null;
	private String fontClass = "subFont";
	private String width = "100%";

	/**
	 * 
	 */
	public CompanyGroupTypeSelectorTag() {
		super();
	}
	
	public static String getParamName(CompanyPersonGroupType groupType) {
		String paramName = "GROUP_" + groupType.getCode();
		return(paramName);
	}
	
	public String makeStartTag() {
		Table table = new Table();
		CompanyPersonRole companyPersonRole = getCompanyPersonRole();
		Person authPerson = ThreadProperties.getPerson();
		
		List groupTypes = CoreServiceLocator.getCompanyService().findCompanyGroupTypes();
		Iterator iter = groupTypes.iterator();
		while (iter.hasNext()) {
			Row row = table.addRow();
			CompanyPersonGroupType groupType = (CompanyPersonGroupType)iter.next();
			UserPrivilegeType requiredUserPrivType = groupType.getPrivilegeWhoCanAssign();
			if (authPerson.hasDomainOrGlobalPrivilege(requiredUserPrivType, ThreadProperties.getDomain())) {
				
				Input checkBox = new Input();
				checkBox.setType(Input.CHECKBOX);
				checkBox.setName(getParamName(groupType));
				
				if ((companyPersonRole != null) && (companyPersonRole.hasGroupMembership(groupType))) {
					checkBox.setChecked(true);
				} else {
					checkBox.setChecked(false);
				}
				Cell cell = row.addCell();
				cell.addText("stdFont", this.getPhrase(groupType.getDescriptionCode()));
	
				cell = row.addCell();
				cell.addElement(checkBox);
			} else {
				Cell cell = row.addCell();
				if ((companyPersonRole != null) && (companyPersonRole.hasGroupMembership(groupType))) {
					cell.addText("stdFont", "Member of group: " + CoreServiceLocator.getPhraseProvider().getMessage(groupType.getDescriptionCode(), authPerson.getLocale().getJavaLocale()));
					Input hidden = new Input();
					hidden.setType(Input.HIDDEN);
					hidden.setName(getParamName(groupType));
					hidden.setValue("checked");
					cell.addElement(hidden);
				} else {
					cell.addText("stdFont", "NOT Member of group: " + CoreServiceLocator.getPhraseProvider().getMessage(groupType.getDescriptionCode(), authPerson.getLocale().getJavaLocale()));
				}
				cell.addText("stdFont", "<br><i>Not authorized to change</i>");
				
			}
			log.debug("Adding blank row");
			row = table.addRow();
			row.addCell().addElement("&nbsp;");
			
		}
		return(table.toString());
	}
	
	
	private CompanyPersonRole getCompanyPersonRole() {
		CompanyPersonRole companyPersonRole = null;
		if (StringUtils.isNullOrBlank(this.getCompanyPersonRoleId())) {
			companyPersonRole = null;
		} else {
			companyPersonRole = CoreServiceLocator.getCompanyService().findCompanyPersonRole(new Long(this.getCompanyPersonRoleId()));
		}
		return(companyPersonRole);
	}
		
	
	/**
	 * @return Returns the alternateColorRow.
	 */
	public boolean getAlternateColorRow() {
		return alternateColorRow;
	}
	/**
	 * @param alternateColorRow The alternateColorRow to set.
	 */
	public void setAlternateColorRow(boolean alternateColorRow) {
		this.alternateColorRow = alternateColorRow;
	}
	/**
	 * @return Returns the maxCompanyCount.
	 */
	public String getMaxCompanyCount() {
		return maxCompanyCount;
	}
	/**
	 * @param maxCompanyCount The maxCompanyCount to set.
	 */
	public void setMaxCompanyCount(String maxCompanyCount) {
		this.maxCompanyCount = maxCompanyCount;
	}
	/**
	 * @return Returns the columnCount.
	 */
	public int getColumnCount() {
		return columnCount;
	}
	/**
	 * @param columnCount The columnCount to set.
	 */
	public void setColumnCount(int columnCount) {
		this.columnCount = columnCount;
	}
	
	private boolean hasBgColor() {
		return(this.showBgColor.toLowerCase().startsWith("tru"));
	}
	/**
	 * @param showBgColor The showBgColor to set.
	 */
	public void setShowBgColor(String showBgColor) {
		this.showBgColor = showBgColor;
	}

	/**
	 * @return Returns the showBgColor.
	 */
	public String getShowBgColor() {
		return showBgColor;
	}

	/**
	 * @return Returns the fontClass.
	 */
	public String getFontClass() {
		return fontClass;
	}

	/**
	 * @param fontClass The fontClass to set.
	 */
	public void setFontClass(String fontClass) {
		this.fontClass = fontClass;
	}

	/**
	 * @return Returns the width.
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width The width to set.
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @return Returns the companyPersonRoleTypeId.
	 */
	public String getCompanyPersonRoleId() {
		return companyPersonRoleId;
	}

	/**
	 * @param companyPersonRoleTypeId The companyPersonRoleTypeId to set.
	 */
	public void setCompanyPersonRoleId(String companyPersonRoleId) {
		this.companyPersonRoleId = companyPersonRoleId;
	}

}
